package com.yanqu.road.server.manger.mammon;

import com.yanqu.road.entity.decoration.MammonTypeSwitch;
import com.yanqu.road.logic.bussiness.config.DecorationBussiness;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;

import java.util.ArrayList;
import java.util.List;

public class MammonTypeSwitchMgr extends TempMgr {

    private static List<MammonTypeSwitch> switchList = new ArrayList<>();

    public static DecorationProto.SyncMammonTypeSwitchRespMsg.Builder getMammonTypeSwitchMsgBuilder() {
        DecorationProto.SyncMammonTypeSwitchRespMsg.Builder builder = DecorationProto.SyncMammonTypeSwitchRespMsg.newBuilder();
        for (MammonTypeSwitch typeSwitch : new ArrayList<>(switchList)) {
            DecorationProto.MammonTypeSwitchMsg.Builder tmp = DecorationProto.MammonTypeSwitchMsg.newBuilder();
            tmp.setChannelId(typeSwitch.getChannelId());
            tmp.setDecorationId(typeSwitch.getDecorationId());
            tmp.setOpenStatus(typeSwitch.getOpenStatus());
            builder.addSwitchList(tmp);
        }
        return builder;
    }

    public static void syncMammonTypeSwitch(GamePlayer player) {
        DecorationProto.SyncMammonTypeSwitchRespMsg.Builder builder = getMammonTypeSwitchMsgBuilder();
        syncMammonTypeSwitch(player, builder);
    }

    public static void syncMammonTypeSwitch(GamePlayer player, DecorationProto.SyncMammonTypeSwitchRespMsg.Builder builder) {
        player.sendPacket(ClientProtocol.U_MAMMON_TYPE_SWITCH_SYNC, builder);
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        int channelId = ServerListMgr.getChannelId(GameServer.getInstance().getServerId());
        switchList = DecorationBussiness.getMammonTypeSwitchDaoImpl(channelId);

        // 同步
        DecorationProto.SyncMammonTypeSwitchRespMsg.Builder builder = getMammonTypeSwitchMsgBuilder();
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            syncMammonTypeSwitch(player, builder);
        }
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
